package com.shawn.recommend.listener;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.shawn.model.mongo.Movement;
import com.shawn.model.mongo.MovementScore;
import org.apache.logging.log4j.util.Strings;
import org.bson.types.ObjectId;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @author shawn
 * @date 2022年 11月 11日 14:47
 */
@Component
public class RecommendMovementListener {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 动态行为监听
     *
     * @param message 消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "touch.movement.queue"
            ,durable = "true"),//持续监听
            exchange = @Exchange(name = "touch.log.exchange",type = ExchangeTypes.TOPIC),
            key = {"log.movement"}
    ))
    public void ListenUserLog(String message){
        //解析数据
        Map map = JSON.parseObject(message, Map.class);
        //1、获取数据
        Long userId = Long.parseLong((String) map.get("userId"));
        String logTime = (String) map.get("logTime");
        String movementId = (String) map.get("busId");
        String type = (String) map.get("type");
        if (Strings.isEmpty(movementId)){
            return;
        }
        //根据动态id查找动态
        Movement movement = mongoTemplate.findById(new ObjectId(movementId), Movement.class);
        if (!Objects.isNull(movement)){
            MovementScore mv = new MovementScore();
            mv.setUserId(userId);
            mv.setDate(System.currentTimeMillis());
            mv.setMovementId(movement.getPid());
            mv.setScore(getMovementScore(type,movement));
            //保存该数据
            mongoTemplate.save(mv);
        }
    }

    /**
     * 动态推荐评分
     *
     * @param type     类型
     * @param movement 运动
     * @return {@link Double}
     */
    private static Double getMovementScore(String type,Movement movement) {
        //0201为发动态  基础5分 50以内1分，50~100之间2分，100以上3分
        //0202为浏览动态， 1
        //0203为动态点赞， 5
        //0204为动态喜欢， 8
        //0205为评论，     10
        //0206为动态取消点赞， -5
        //0207为动态取消喜欢   -8
        double score = 0d;
        switch (type) {
            case "0201":
                score = 5d;
                score += movement.getMedias().size();
                int length = StrUtil.length(movement.getTextContent());
                if (length >= 0 && length < 50) {
                    score += 1;
                } else if (length < 100) {
                    score += 2;
                } else {
                    score += 3;
                }
                break;
            case "0202":
                score = 1d;
                break;
            case "0203":
                score = 5d;
                break;
            case "0204":
                score = 8d;
                break;
            case "0205":
                score = 10d;
                break;
            case "0206":
                score = -5d;
                break;
            case "0207":
                score = -8d;
                break;
            default:
                break;
        }
        return score;
    }


}
